package SocialMediaSentiment;

import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import jxl.write.WriteException;

public class testStructures {

	public static void main(String[] args) throws IOException, WriteException {
		HashMap<Integer, String[]> tweetsWithSentiment = ReadExcel.read(
				"CorrectedTweets.xls", 0, 1);
		Iterator<Integer> k = (Iterator<Integer>) tweetsWithSentiment.keySet()
				.iterator();
		HashMap<String, List<String>> lijsten = StandardFunctions
				.getWoordenlijsten();
		List<String> tweetStats;
		HashMap<String, String> toprint = new HashMap<String, String>();
		int zero = 0;
		boolean printer;
		while (k.hasNext()) {
			printer = false;
			tweetStats = new ArrayList<String>();
			int index = k.next();
			String[] data = tweetsWithSentiment.get(index);
			String tweet = data[0];
			int sentiment = Integer.parseInt(data[1].replace(" ", ""));

			List<String[]> trigrams = StandardFunctions.getTriGramsList(tweet);
			System.out.println(tweet + "  " + data[1]);
			for (String[] trigram : trigrams) {
				String trigramclass = StandardFunctions.deterimeTrigramStructures(
						trigram, lijsten);
				if(trigramclass.equals("STOP"))
					break;
				tweetStats.add(trigramclass);
				 System.out.println("Trigram: " + trigram[0] + " "
				 + trigram[1] + " " + trigram[2] + " -->  "
				 + trigramclass);
			}
			int tweetValue = calcTweetValue(tweetStats);
			System.out.println("Classified as: "+tweetValue);
			
			if(sentiment == 0)
				zero++;
			if(sentiment == 0 && tweetValue != 0)
				printer = true;
				/*
			if (tweetValue > 0 && sentiment <= 0) {
				printer = true;
			} else if (tweetValue == 0 && sentiment != 0) {
				printer = true;
			} else if (tweetValue < 0 && sentiment >= 0) {
				printer = true;
			}*/

			if (printer == true) {
				String trigramprint = "";
				for (String trigramStat : tweetStats) {
					trigramprint = trigramprint + " " + trigramStat;
				}
				toprint.put("" + tweet + "" + sentiment, trigramprint);
			}

		}
		writeExcel.write("wrongTrigramClassification.xls", toprint);
		System.out.println(zero);
	}

	private static int calcTweetValue(List<String> tweetStats) {
		boolean ontkenningReset = false;
		int ontkenningFound = 0;
		int negatief = 0;
		int positief = 0;

		int size = tweetStats.size();
		
		for (int i = 0; i < tweetStats.size(); i++) {
			switch (tweetStats.get(i)) {
			case "+":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if (ontkenningFound > 0) {
					negatief++;
					ontkenningFound--;
				} else
					positief++;
				break;
			case "++":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if (ontkenningFound > 0) {
					negatief++;
					ontkenningFound--;
				} else
					positief += 2;
				break;
			case "+++":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if (ontkenningFound > 0) {
					negatief++;
					ontkenningFound--;
				} else
					positief += 3;
				break;
			case "-":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if (ontkenningFound > 0) {
					ontkenningFound--;
				} else
					negatief++;
				break;
			case "--":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if (ontkenningFound > 0) {
					ontkenningFound--;
				} else
					negatief += 2;
				break;
			case "---":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if (ontkenningFound > 0) {
					ontkenningFound--;
				} else
					negatief += 3;
				break;
			case "o":
				ontkenningFound ++;
				break;
			case "o-":
				positief++;
				break;
			case "o+":
				negatief++;
				break;
			case "-o":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if(ontkenningFound > 0)
				{
					positief++;
				}
				else
				{
				negatief++;
				ontkenningFound++;
				}
				break;
			case "+o":
				if (ontkenningReset == true){
					ontkenningFound = 0;
					ontkenningReset = false;
				}
				if(ontkenningFound > 0)
				{
					negatief++;
				}
				else{				
				positief++;
				ontkenningFound++;
				}
				break;
			case "N"://na neutrale trigram geldt ontkenning niet meer
				ontkenningReset = true;
				break;
			case "RESET"://maar reset de tweetStat
				size = tweetStats.size()-(i+1);
				positief = 0;
				negatief = 0;
			}
		}
		System.out.println(size + "  " + positief + "  " + negatief + " " + ontkenningFound);
		
		negatief += ontkenningFound;
		if( Math.abs(negatief - positief)  <=3 && size >= 12)
			return 0;
		
		if (negatief > positief)
			return -1;
		else if (positief > negatief)
			return 1;
		else
			return 0;

	}

}
